const init_component = (item) => {
  return new Promise((resolve, reject) => {
    fetch(item.src).then((res) => {
      res.text().then((html) => {
        let com = document.createElement("div");
        com.innerHTML = html.split("<script")[0];
        document.body.appendChild(com);
        let comjs = document.createElement("script");
        let jscode = html.split("comjs>")[1].split("</script>")[0];
        comjs.innerHTML = jscode;
        document.body.appendChild(comjs);
        resolve();
        setTimeout(() => {
          // 执行后清除代码
          comjs.remove();
          com.remove();
        }, 300);
      });
    });
  });
};

const register_components = (list) => {
  return Promise.all(list.map((val) => init_component(val)));
};

window.__components = register_components([
  {
    src: "/components/my-menus.html",
  },
  {
    src: "/components/my-select.html",
  },
]);
